package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.CacReturnDto;
import com.csun.cmny.provider.constant.Constant;
import com.csun.cmny.provider.dao.OldmanCheckInMapper;
import com.csun.cmny.provider.dao.OldmanDeviceMapper;
import com.csun.cmny.provider.dao.OldmanItemMapper;
import com.csun.cmny.provider.dao.SetMealMapper;
import com.csun.cmny.provider.model.dto.OldManBirthdayDto;
import com.csun.cmny.provider.model.vo.OldmanRpcVo;
import com.csun.cmny.provider.pojo.*;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.CacRpcService;
import com.csun.cmny.provider.service.OldmanCheckInService;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.vo.CheckinUserInfoVo;
import com.csun.cmny.provider.vo.OldmanBedInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("OldmanCheckInService")
public class OldmanCheckInServiceImpl implements OldmanCheckInService {

    @Resource
    OldmanCheckInMapper oldmanCheckInMapper;
    @Resource
    OldmanDeviceMapper oldmanDeviceMapper;
    @Resource
    OldmanItemMapper oldmanItemMapper;
    @Resource
    SetMealMapper setMealMapper;
    @Autowired
    private OldmanService oldmanService;
    @Autowired
    private CacRpcService cacRpcService;
    @Autowired
    private BedService bedService;

    @Override
    public int insert(OldmanCheckIn oldmanCheckIn) {
        int n = oldmanCheckInMapper.insertSelective(oldmanCheckIn);
        return n > 0 ? oldmanCheckIn.getOldmanId() : n;
    }

    @Override
    public int deleteByOldId(Integer oldmanId) {
        return oldmanCheckInMapper.deleteByPrimaryKey(oldmanId);
    }

    @Override
    public int insertOldDeviceBatch(List<OldmanDevice> oldmanDeviceList) {
        return oldmanDeviceMapper.insertBatch(oldmanDeviceList);
    }

    @Override
    public int insertOldItemBatch(List<OldmanItem> oldmanItemList) {
        return oldmanItemMapper.insertBatch(oldmanItemList);
    }

    @Override
    public OldmanCheckIn selectById(Integer oldmanId) {
        return oldmanCheckInMapper.selectByPrimaryKey(oldmanId);
    }

    @Override
    public int delOldDeviceByOid(Integer oldmanId) {
        return oldmanDeviceMapper.delOldDeviceByOid(oldmanId);
    }

    @Override
    public int delOldItemByOid(Integer oldmanId) {
        return oldmanItemMapper.delOldItemByOid(oldmanId);
    }

    @Override
    public CheckinUserInfoVo selectByOid(Integer oldmanId) {
        return oldmanCheckInMapper.selectByOid(oldmanId);
    }

    @Override
    public List<OldmanDevice> sltOldDeviceByOid(Integer oldmanId) {
        return oldmanDeviceMapper.sltOldDeviceByOid(oldmanId);
    }

    @Override
    public List<Map<String, Object>> sltOldItemByOid(Integer oldmanId) {
        return oldmanItemMapper.sltOldItemByOid(oldmanId);
    }

    @Override
    public List<Map<String, Object>> showAll(Map<String, Object> queryMap) {
        return oldmanCheckInMapper.showAllOld(queryMap);
    }

    @Override
    public List<OldmanInfo> showCheckInOldManInfo(Map<String, Object> queryMap) {
        return oldmanCheckInMapper.showCheckInOldManInfo(queryMap);
    }

    @Override
    public List<SetMeal> sltSetMealsByOid(int oldmanId) {
        return setMealMapper.sltSetMealsByOid(oldmanId);
    }

    @Override
    public List<SexCountVo> getSexAnalysis(Long homeId) {
        return oldmanCheckInMapper.getSexAnalysis(homeId);
    }

    @Override
    public List<SexCountVo> gerOldTypeAnalysis(Long homeId) {
        return oldmanCheckInMapper.gerOldTypeAnalysis(homeId);
    }

    @Override
    public SexCountVo getOldmanNalysis(Map map) {
        return oldmanCheckInMapper.getOldmanNalysis(map);
    }

    @Override
    public List<BirthdayRemind> getOldBirthdayRemind(Map<String, Object> queryMap) {
        return oldmanCheckInMapper.getOldBirthdayRemind(queryMap);
    }

    @Override
    public int update(OldmanCheckIn oldmanCheckIn) {
        return oldmanCheckInMapper.updateByPrimaryKey(oldmanCheckIn);
    }

    @Override
    public int updateSelective(OldmanCheckIn oldmanCheckIn) {
        return oldmanCheckInMapper.updateByPrimaryKeySelective(oldmanCheckIn);
    }

    @Override
    public int outByOldmanId(Integer oldmanId) {
        return oldmanCheckInMapper.outByOldmanId(oldmanId);
    }

    @Override
    public Map<String, Object> queryoldManAllInfo(Integer oldmanId) {
        Map<String, Object> result = new LinkedHashMap<>(16);
        Oldman oldman = new Oldman();
        oldman.setOldmanId(oldmanId);
        //查询老人详细信息
        Oldman oldManInfo = oldmanService.selectDescByOld(oldman);
        result.put("oldManInfo", oldManInfo);
        //查询老人评估详情
        List<CacReturnDto> cacDesc = cacRpcService.selectCacOldMnr(oldmanId);
        result.put("cacDesc", cacDesc);
        //查询老人入院信息
        OldmanCheckIn oldmanCheckIn = selectById(oldmanId);
        if (oldmanCheckIn != null) {
            result.put("oldmanCheckIn", oldmanCheckIn);
            //查询老人餐饮费
            if (oldmanCheckIn.getSetMealId() != null) {
                SetMeal oldManSetMeal = setMealMapper.selectById(oldmanCheckIn.getSetMealId());
                result.put("oldManSetMeal", oldManSetMeal);
            }
            //判断老人是否为入院老人
            if (oldmanCheckIn.getOldType() != null && oldmanCheckIn.getOldType() == Constant.RUYUAN) {
                //床位信息
                OldmanBedInfoVo oldmanBedInfo = bedService.selectByOid(oldmanId);
                if (oldmanBedInfo != null) {
                    result.put("oldmanBedInfo", oldmanBedInfo);
                }
            }
        }
        //查询老人个性化套餐
        List<Map<String, Object>> individuation = sltOldItemByOid(oldmanId);
        if (individuation != null && individuation.size() > 0) {
            result.put("individuation", individuation);
        }
        return result;
    }

    @Override
    public List<OldmanRpcVo> getCheckinByHomeId(Integer homeId) {
        return oldmanCheckInMapper.getCheckinByHomeId(homeId);
    }

    /**
     * select count by set meal id
     *
     * @param setMealId set meal id
     * @return the int
     */
    @Override
    public int selectCountBySetMealId(Integer setMealId) {
        return oldmanCheckInMapper.selectCountBySetMealId(setMealId);
    }

    @Override
    public List<CheckinUserInfoVo> selectGroupId(){
        return oldmanCheckInMapper.selectGroupId();
    }

    @Override
    public List<BirthdayRemind> getNewOldBirthdayRemind(OldManBirthdayDto oldManBirthdayDto) {
        return oldmanCheckInMapper.getNewOldBirthdayRemind(oldManBirthdayDto);
    }
}
